//
//  RigistViewController.m
//  jang
//
//  Created by lee han on 03/04/2017.
//  Copyright © 2017 lee han. All rights reserved.
//

#import "RigistViewController.h"
#import "CommonView.h"
#import "CRUserData.h"

@interface RigistViewController ()

@end

@implementation RigistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationItem.backBarButtonItem setTitle:@""];
    
    _mTextPhone.delegate = self;
    _mTextPassword.delegate = self;
    _mTextConfirm.delegate = self;
    _mTextVerification.delegate = self;
    _mTextInvitation.delegate = self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)onClickVerification:(id)sender {
    //获取验证码
    if(_mTextPhone.text.length != 11){
        [CommonView addAlertView:self Text:@"手机号输入有误"];
        return;
    }
    
    NSMutableDictionary* data = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                 _mTextPhone.text,  @"phone",
                                 nil];
    
    [[network sharedInstance] startHttpRequest:self Data:data functionType:NetworkFunctionType_GetVerifyCode];
}
- (IBAction)onClickSubmit:(id)sender {
    if(_mTextPhone.text.length != 11){
        [CommonView addAlertView:self Text:@"手机号输入有误"];
        return;
    }
    
    if(_mTextPassword.text.length == 0){
        [CommonView addAlertView:self Text:@"请输入密码"];
        return;
    }
    
    if(_mTextConfirm.text != _mTextPassword.text){
        [CommonView addAlertView:self Text:@"密码输入不一致"];
        return;
    }
    
    if(_mTextVerification.text.length == 0){
        [CommonView addAlertView:self Text:@"请输入验证码"];
        return;
    }
    
    NSString* invitation = _mTextInvitation.text;
    if(invitation == nil || invitation.length == 0){
        invitation = @"0";
    }
    NSMutableDictionary* data = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                 _mTextPhone.text,  @"username",
                                 _mTextPassword.text, @"pwd",
                                 _mTextVerification.text, @"vcode",
                                 invitation, @"icode",
                                 nil];
    
    [[network sharedInstance] startHttpRequest:self Data:data functionType:NetworkFunctionType_Signup];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark networkDelegate
- (void)parserResult:(NSDictionary *)response functionType:(NSInteger)type
{
    if (type == NetworkFunctionType_Signup) {
        if([[response objectForKey:@"status"] integerValue] == 1){
            [CRUserData sharedInstance].m_uid = [response objectForKey:@"uid"];
            [CRUserData sharedInstance].m_token = [response objectForKey:@"token"];
            [CRUserData sharedInstance].m_money = [[response objectForKey:@"money"] integerValue];
            //登陆成功
            [CRUserData sharedInstance].m_landingPhone = self.mTextPhone.text;
            [CRUserData sharedInstance].m_password = self.mTextPassword.text;
            [[CRUserData sharedInstance] saveLoginInfo];
            
            
            [[CRUserData sharedInstance] setCollectionData:[response objectForKey:@"collect"]];
            [[CRUserData sharedInstance] setShopCarData:[response objectForKey:@"shopcar"]];
            
            [[CRUserData sharedInstance] setHomeData:[response objectForKey:@"homedata"]];
            
            [CommonView gotoViewController:self Target:@"main"];
        }
        else{
            [CommonView addAlertView:self Text:@"账号已存在"];
        }
    }
    else if (type == NetworkFunctionType_GetVerifyCode) {
        if([[response objectForKey:@"status"] integerValue] != 1) {
            [CommonView addAlertView:self Text:@"获取验证码失败"];
        }
    }
}
//end networkDelegate

#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if(textField == _mTextPhone){
        [_mTextPassword becomeFirstResponder];
    }
    else if (textField == _mTextPassword){
        [_mTextConfirm becomeFirstResponder];
    }
    else if (textField == _mTextConfirm){
        [_mTextInvitation becomeFirstResponder];
    }
    else if (textField == _mTextInvitation){
        [_mTextVerification becomeFirstResponder];
    }
    else if (textField == _mTextVerification){
        [textField resignFirstResponder];
    }
    return YES;
}
//end
@end
